get-nws-alerts.pl 

Perl script grabbing NWS XML feeds

code=yes
# tags = #perl - #weather - #xml - #rss
showintro=no

########

#!/usr/bin/perl -wT

use strict;
use Data::Dumper;
use HTML::Entities;

$|++;

BEGIN {
    unshift @INC, "/home/toledotk/New-TT/lib";
}

use XML::TreePP;
use Config::Config;

my $show_fulton_county_msg = Config::get_value_for("show_fulton_county_msg");
my $fulton_county_msg = Config::get_value_for("fulton_county_msg");

my $show_hazardous_outlook = Config::get_value_for("show_hazardous_outlook");

my $show_mesoscale_discussions = Config::get_value_for("show_mesoscale_discussion");

my $show_radar_link = Config::get_value_for("show_radar_link");
my $radar_gif_url   = Config::get_value_for("radar_link");

my $bgcolor = Config::get_value_for("weather_alert_bgcolor");

if ( !$bgcolor ) {
    $bgcolor = "white";
}

# my $tt_alert_link = "<a title=\"Toledo Talk Weather Page\" href=\"http://toledotalk.com/cgi-bin/tt.pl/article/138464/Toledo_area_weather_alerts\">>></a>";

my $tt_alert_link = "<a title=\"Toledo Talk Weather Page\" href=\"http://toledotalk.com/weather/html/wx.html\">>></a>";

my $radar_link  = "<a href=\"" . $radar_gif_url     . "\">weather radar</a> - ";

my $fulton_link = "<a href=\"" . $fulton_county_msg . "\">fulton county</a> - ";

if ( !$show_fulton_county_msg ) {
    $fulton_link = "";
}

if ( !$show_radar_link ) {
    $radar_link = "";
}

my $have_something = 0;

my $final_link = "";

        #processing spc mesoscale discussions. if CLE then display at TT
 my $md_link = "";
 if ( $show_mesoscale_discussions ) {
    my $md_url = "http://www.spc.noaa.gov/products/spcmdrss.xml";
    my $mdtpp = XML::TreePP->new();
    my $mdtree = $mdtpp->parsehttp( GET => $md_url );
    if ( $mdtree ) {
        # reference to an array of hasshes
        # print $mdtree->{'rss'}->{'channel'}->{'item'}->[0]->{'link'} . "\n";
     my $mdarrref = $mdtree->{'rss'}->{'channel'}->{'item'};
     if ( ref $mdarrref eq ref [] ) {
        foreach my $mditem ( @$mdarrref ) {
            my $mdlink = $mditem->{'link'};
            my $mddesc = $mditem->{'description'};
#            if ( $mddesc =~ m/CLE\.\./s ) {
            if ( $mddesc =~ m/CLE/s  or  $mddesc =~ m/DTX/s  or  $mddesc =~ m/IWX/s ) {
               $md_link .= " <a href=\"$mdlink\">mesoscale discussion</a> - ";
               $have_something = 1;
               $final_link .= " <a href=\"$mdlink\">mesoscale discussion</a> - ";
            } 
        }
      } else {
            my $mdlink = $mdarrref->{'link'};
            my $mddesc = $mdarrref->{'description'};
#            if ( $mddesc =~ m/CLE\.\./s ) {
            if ( $mddesc =~ m/CLE/s  or  $mddesc =~ m/DTX/s  or  $mddesc =~ m/IWX/s ) {
               $md_link .= " <a href=\"$mdlink\">mesoscale discussion</a> - ";
               $have_something = 1;
               $final_link .= " <a href=\"$mdlink\">mesoscale discussion</a> - ";
            } 
      }    
    }
  }

       #lucas county zone
    #### my $xml_url =  "http://forecast.weather.gov/MapClick.php?lat=41.61000&lon=-83.8&unit=0&lg=english&FcstType=dwml";
   my $xml_url = "http://forecast.weather.gov/MapClick.php?lat=41.66394&lon=-83.55521199999998&unit=0&lg=english&FcstType=dwml";  

       #fulton county zone for testing on 28Jan2013
#    my $xml_url =  "http://forecast.weather.gov/MapClick.php?lat=41.56533&lon=-84.17949152542unit=0&lg=english&FcstType=dwml";

    my $tpp = XML::TreePP->new();
    my $tree = $tpp->parsehttp( GET => $xml_url );
    exit unless $tree;

#  print Dumper($tree);

    my $alert_msg = "";

    # reference to an array of hashes - possibly unless only one hazard message exists and then it's not an array.
    my $test = $tree->{'dwml'}->{'data'}->[0]->{'parameters'}->{'hazards'};

    if ( ref $test ne ref [] ) {
        my $hazard_headline_one = lc($tree->{'dwml'}->{'data'}->[0]->{'parameters'}->{'hazards'}->{'hazard-conditions'}->{'hazard'}->{'-headline'});
        my $hazard_url_one      = $tree->{'dwml'}->{'data'}->[0]->{'parameters'}->{'hazards'}->{'hazard-conditions'}->{'hazard'}->{'hazardTextURL'};
        if ( $hazard_headline_one and $hazard_url_one ) {
            if ( $hazard_headline_one eq "hazardous weather outlook" && $show_hazardous_outlook eq "1" ) {
                # print "<div id=\"weatheralerts$bgcolor\"><a title=\"National Weather Service Statement\" href=\"$hazard_url_one\">$hazard_headline_one</a> - $md_link $fulton_link $radar_link $tt_alert_link</div>\n";
                $have_something = 1;
                $final_link .= " <a title=\"National Weather Service Statement\" href=\"$hazard_url_one\">$hazard_headline_one</a> - \n";
            } elsif ( $hazard_headline_one ne "hazardous weather outlook" ) {
                # print "<div id=\"weatheralerts$bgcolor\"><a title=\"National Weather Service Statement\" href=\"$hazard_url_one\">$hazard_headline_one</a> - $md_link $fulton_link $radar_link $tt_alert_link</div>\n";
                $final_link .= " <a title=\"National Weather Service Statement\" href=\"$hazard_url_one\">$hazard_headline_one</a> - \n";
                $have_something = 1;
            }
        }
    } else {

        my $tmp_ctr = 0;

        # looping through the reference to the array of hashes
        foreach my $hz ( @$test ) {
            # each hz in the loop is a hash
            my $hazard     = lc($hz->{'hazard-conditions'}->{'hazard'}->{'-headline'}); 
            my $hazard_url = $hz->{'hazard-conditions'}->{'hazard'}->{'hazardTextURL'};

            if ( $tmp_ctr > 0 ) {
                $alert_msg .= " - ";
            }

            if ( $hazard eq "hazardous weather outlook" && $show_hazardous_outlook eq "1" ) {
                $alert_msg .= "<a title=\"National Weather Service Statement\" href=\"$hazard_url\">$hazard</a>";
                $final_link .= " <a title=\"National Weather Service Statement\" href=\"$hazard_url\">$hazard</a> - \n";
                $have_something = 1;
            } elsif ( $hazard ne "hazardous weather outlook" ) {
                $alert_msg .= "<a title=\"National Weather Service Statement\" href=\"$hazard_url\">$hazard</a>";
                $final_link .= " <a title=\"National Weather Service Statement\" href=\"$hazard_url\">$hazard</a> - \n";
                $have_something = 1;
            }
            $tmp_ctr++;
        }

        if ( $tmp_ctr > 0 ) {
            # print "<div id=\"weatheralerts$bgcolor\">$alert_msg - $md_link $fulton_link $radar_link $tt_alert_link</div>\n";
            $have_something = 1;
        }
    } 

    if ( !$have_something and ( $show_radar_link or $show_fulton_county_msg )  ) {
        # print "<div id=\"weatheralerts$bgcolor\">$fulton_link $radar_link $tt_alert_link</div>\n";
    }

    if ( $final_link ) {
        $final_link = "<div id=\"weatheralerts$bgcolor\">$final_link $fulton_link $radar_link $tt_alert_link</div>\n";
        print $final_link;
    }